package com.example.StudentComponent;


import com.example.Utils.HttpUtil;
import com.example.Utils.ResultUtil;
import com.example.pojo.Course;
import com.fasterxml.jackson.databind.introspect.AnnotationCollector;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;

import java.util.Vector;

public class StudentCourse extends Box{

    JTable Table;
    String[][] students=new String[6][6];
    TableModel tm;


    //构造方法
    public StudentCourse(String user) {
        super(BoxLayout.Y_AXIS);
        //组装
        JPanel jPanel = new JPanel();
        jPanel.setBackground(new Color(208, 210, 213, 179));
        jPanel.setMaximumSize(new Dimension(800, 50));


        //组装表格
        String[] ss = {"节次","星期一", "星期二", "星期三", "星期四", "星期五","星期六","星期日"};
        requestGetData("http://localhost:8080/StudentCourse/"+user);
        tm = new DefaultTableModel(students, ss);
        Table = new JTable(tm) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        Table.setRowHeight(70);

        //只能选中一行
        Table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        TableViewRenderer tv=new TableViewRenderer();
        DefaultTableCellRenderer r=new DefaultTableCellRenderer();
        Table.setDefaultRenderer(Object.class, tv);//设置渲染器
        //列大小不变
        Table .getTableHeader().setResizingAllowed(false);
        //列不可移动
        JScrollPane scrollPane = new JScrollPane(Table);
        Table.setShowGrid(true);
        Table.setFont(new Font("宋体", Font.PLAIN, 15));
        Table.getTableHeader().setReorderingAllowed(false);
        this.add(scrollPane);
    }





    public  void requestGetData( String url) {
        String result = HttpUtil.sendGet(url);
        Vector<Course> courses = ResultUtil.ResultVectorCourse(result);
        for (int i = 0; i < 6; i++) {
            students[i][0]=i+1+"";
        }
        for (int i = 0; i < courses.size(); i++) {
            students[classTime1(courses.get(i))] [classTime2(courses.get(i))]=courses.get(i).getName() +"\n"+courses.get(i).getClassRoom();

        }
    }

    public int classTime2(Course course){
        if (course.getPeriod().equals("星期一")) {
            return 1;
        }
        if (course.getPeriod().equals("星期二")) {
            return 2;
        }
        if (course.getPeriod().equals("星期三")) {
            return 3;
        }
        if (course.getPeriod().equals("星期四")) {
            return 4;
        }
        if (course.getPeriod().equals("星期五")) {
            return 5;
        }
        return 5;
    }

    public int classTime1(Course course){
        if (course.getClassTime().equals("8:00-9:40")) {
            return 0;
        }
        if (course.getClassTime().equals("10:00-11:40")) {
            return 1;
        }
        if (course.getClassTime().equals("14:00-15:40")) {
            return 2;
        }
        if (course.getClassTime().equals("16:00-17:40")) {
            return 3;
        }
        if (course.getClassTime().equals("19:00-20:40")) {
            return 4;
        }
        return 5;
    }




}
